package com.dm.cloud.images.schedule;

import cn.hutool.core.util.ReUtil;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.Frame;
import com.dm.cloud.images.util.StringUtil;


public class ContainerLogRegexResultCallback extends ResultCallback.Adapter<Frame> {

    /**
     * 正则条件
     */
    private String regexCondition;

    public ContainerLogRegexResultCallback(String regexCondition){
        this.regexCondition=regexCondition;
    }

    /**
     * 日志状态
     * 0正在监听  1已正确识别条件  2发生了错误
     */
    private int logStatus=0;

    private String errStr;

    public int getLogStatus(){
        return logStatus;
    }
    public String getErrStr(){
        return errStr;
    }

    @Override
    public void onNext(Frame object) {
        final byte[] payload = object.getPayload();
        String text= StringUtil.replaceANSI(new String(payload));
//        String text= StringUtil.replaceANSI(object.toString());
        if(ReUtil.contains(regexCondition,text)){
            logStatus=1;
        }
    }

    @Override
    public void onError(Throwable throwable) {
        logStatus=2;
        errStr=throwable.getMessage();
    }

}
